package com.kye.express.business.base;

import android.arch.lifecycle.Lifecycle;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.View;

import com.trello.lifecycle2.android.lifecycle.AndroidLifecycle;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.LifecycleTransformer;

public class BaseAppCompatDialogFragment extends DialogFragment {

    private final LifecycleProvider<Lifecycle.Event> provider
            = AndroidLifecycle.createLifecycleProvider(this);

    public <T> LifecycleTransformer<T> bindToLifecycle() {
        return provider.bindToLifecycle();
    }

    public <T> LifecycleTransformer<T> bindToLifecycleDestroy() {
        return provider.bindUntilEvent(Lifecycle.Event.ON_DESTROY);
    }

    public <T> LifecycleTransformer<T> bindToLifecycleEvent(Lifecycle.Event event) {
        return provider.bindUntilEvent(event);
    }

    public LifecycleProvider<Lifecycle.Event> getProvider() {
        return provider;
    }

    protected boolean isViewCreated;
    protected boolean isViewVisible;
    protected boolean isDataLoadStarted;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isViewCreated = true;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData(savedInstanceState);
        prepareFetchData(savedInstanceState);
    }

    /**
     * Fragment创建后正常初始化数据
     *
     * @param savedInstanceState
     */
    protected void initData(@Nullable Bundle savedInstanceState) {

    }

    /**
     * ViewPager Fragment创建后,并且显示界面才初始化数据,避免initData和initDataFirstTiem重复加载
     * @param isForceUpdate
     * @param savedInstanceState
     */
    protected void initDataFirstTime(boolean isForceUpdate, @Nullable Bundle savedInstanceState) {

    }

    /**
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isViewVisible = isVisibleToUser;
        /**
         * 其他情况导致重新显示时，也要判断是否应该获取数据
         */
        prepareFetchData(null);
    }

    /**
     * Fragment首次进入加载数据,默认不强制刷新
     *
     * @return
     */
    public boolean prepareFetchData(@Nullable Bundle savedInstanceState) {
        return prepareFetchData(false, savedInstanceState);
    }

    /**
     * @param isForceUpdate
     * @return
     */
    public boolean prepareFetchData(boolean isForceUpdate, @Nullable Bundle savedInstanceState) {
        if (isViewCreated && isViewVisible && (!isDataLoadStarted || isForceUpdate)) {
            initDataFirstTime(isForceUpdate, savedInstanceState);
            isDataLoadStarted = true; // 已经开始了首次加载数据
            return true;
        }
        return false;
    }

}
